package com.grf.hadoop.flow_bean;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class FlowBean implements Writable, WritableComparable<FlowBean> {

    private long upFlow; //上行流量
    private long downFlow; //下行流量
    private long sumFlow; //总流量

    //空参构造，为了后续反射用
    public FlowBean() {
        super();
    }

    public FlowBean(long upFlow,long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = upFlow + downFlow;
    }

    /**
     * 重写
     * @param dataOutput
     * @throws IOException
     */
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(upFlow);
        dataOutput.writeLong(downFlow);
        dataOutput.writeLong(sumFlow);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        //顺序一致
        upFlow = dataInput.readLong();
        downFlow = dataInput.readLong();
        sumFlow = dataInput.readLong();
    }

    @Override
    public String toString() {

        return upFlow + " " + downFlow  + " " + sumFlow;
    }

    public void set(long sumUpFlow,long sumDownFlow) {
        this.sumFlow = sumDownFlow + sumUpFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public long getUpFlow() {
        return upFlow;
    }

    public long getSumFlow() {
        return sumFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }


    @Override
    public int compareTo(FlowBean flowBean) {
        return this.sumFlow > flowBean.getSumFlow() ? -1 : 1;
    }
}
